package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Database {
	private volatile static Database instance;
	private String userName;
	private String password;
	private String host;
	private String port;
	private String dbName;
	private String url;

	private Database() throws ClassNotFoundException {
		this.userName = "root";
		this.password = "0906";
		this.host = "localhost";
		this.port = "3306";
		this.dbName = "storemanager";
		this.url = new StringBuilder("jdbc:mysql://").append(host).append(":")
				.append(port).append("/").append(dbName).toString();
		Class.forName("com.mysql.jdbc.Driver");
	}

	public static Database getInstance() throws ClassNotFoundException {
		if (instance == null) {
			synchronized (Database.class) {
				if (instance == null) {
					instance = new Database();
				}
			}
		}
		return instance;
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, userName, password);
	}

	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {
		Database db = Database.getInstance();
		System.out.println(db.getConnection());
		System.out.println(db.getConnection());
	}

}
